
use verklegt

go 

CREATE FUNCTION [dbo].RemoveUnderscore ( @InputString varchar(4000) ) 
RETURNS VARCHAR(4000)
AS
BEGIN

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = ''
SET @Index = 1

WHILE @Index <= LEN(@InputString)
BEGIN
    SET @Char     = SUBSTRING(@InputString, @Index, 1)    

    IF @Char != '_'
    BEGIN
        set @OutputString = @OutputString + @Char
    END

    SET @Index = @Index + 1
END

RETURN @OutputString

END


go

CREATE FUNCTION [dbo].[InitCap] ( @InputString varchar(4000) ) 
RETURNS VARCHAR(4000)
AS
BEGIN

DECLARE @Index          INT
DECLARE @Char           CHAR(1)
DECLARE @PrevChar       CHAR(1)
DECLARE @OutputString   VARCHAR(255)

SET @OutputString = LOWER(@InputString)
SET @Index = 1

WHILE @Index <= LEN(@InputString)
BEGIN
    SET @Char     = SUBSTRING(@InputString, @Index, 1)
    SET @PrevChar = CASE WHEN @Index = 1 THEN '_'
                         ELSE SUBSTRING(@InputString, @Index - 1, 1)
                    END
    

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
    BEGIN
        IF (@PrevChar != '''' OR UPPER(@Char) != 'S') --and (@Index != 1)
            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))
    END

    SET @Index = @Index + 1
END

RETURN dbo.RemoveUnderscore(@OutputString)

END

go

select dbo.InitCap('this_is_krapp')

go
use master
go
